<template>
	<view class="index">
		<custom id='head' titleColor='#181818' navBackgroundColor='#FFFFFF' :style="{height:cusHeight+'px'}"
			textContent='活动列表'></custom>
		<view class="main-container" :style="{height:heightc+'px'}">
			<scroll-view v-if="actList.length > 0" @scrolltolower="scrollLower" @refresherrefresh="onRefresh"
				style="height: 100%;" scroll-y>
				<captureInfor v-for="item in actList" :info="item" @qrCode='qrCodeApp' />
			</scroll-view>
			<view v-else class="noData" @click="goPublish">gopublish！</view>
			<view class="" @click="setFun">setFun！</view>
		</view>
	</view>
</template>

<script>
	import {
		getCache,
		setCache,
		apiUrl,
		yfAPIS
	} from "@/utils/cache.js";
	import captureInfor from '../../common/capture.vue'
	import {
		getImage,
		getName,
		systemInfo
	} from '@/utils/index.js';
	import custom from '../../colorui/commonpent/custom.vue'
	import {
		MQTT_OPTIONS,
		MQTT_IP
	} from '../../utils/mqttConfig.js'
	import request from "@/utils/request.js";
	// import SM4Util from '@/utils/sm4.js'
	// import { SM4Util } from 'sm4util'
	import SM4Util from '@/utils/sm4/index.js'
	export default {
		data() {
			return {
				title: 'Hello',
				data: 0,
				heightc: 0,

				triggered: false,
				client: null,
				switches: 'admin_id',
				
				actList: [],
				faceUrl:"",
				secretKey:'trE4n9TqGZyiC5aP',
			}
		},
		components: {
			custom,
			captureInfor
		},
		onReady() {
			var that = this;
			uni.getSystemInfo({
				success: function(res) {
					let view = uni.createSelectorQuery().select("#head"); //局部滑块
					view.boundingClientRect(data => {
						that.heightc = res.windowHeight - data.height - 28;
					}).exec();
				},
			});
		},
		onShow() {
			
			//this.getDataActivity();
			
			
			//const miStr = SM4Util.sm4ECBEncrypt('13012345678', '93F3044B07393417') // 加密 SM4Util.sm4ECBEncrypt(需要加密的字符串, 密钥)
			//console.log('miStr----', miStr) // 8O/HeW8uoXU/CkcDaXRpxQ==
			//YfLeZ  登记码 
			// const miStr = 'YfLeZNKNPuSStQ8U+TDWc9v9gghBydzldYCN3eBiUtrRzMOT7BHRapKQvaGw+8a2sntST';
			// const typeData = miStr.substr(0,5);
			// const codeInfo = miStr.slice(5);
			// console.log('jieStr----截取', codeInfo) 
			// console.log('jieStr----数据', codeInfo) // 13012345678
			// const jieStr = SM4Util.sm4ECBDecrypt(codeInfo, 'trE4n9TqGZyiC5aP') // 解密 SM4Util.sm4ECBDecrypt(加密后的字符串, 密钥)
			// console.log('jieStr----', jieStr) // 13012345678
			// let m = '1690508892|刘绪槟|440582199802265153';
			// const mm = m.split('|');
			// console.log(mm)
		},
		computed: {
			cusHeight() {
				let height = this.CustomBar / (uni.upx2px(this.CustomBar) / this.CustomBar) + 220;
				return this.CustomBar;
			}
		},

		async onLoad() {

		},
		created() {
			window.setFun = this.setFun;
		},
		//下拉刷新
		onPullDownRefresh() {
			//this.getDataActivity();
			this.triggered = true;

		},
		//上拉加载
		onReachBottom() {
			this.triggered = true;
			console.log('上拉刷新2');
		},

		methods: {
			setFun(arg){
				   console.log("获取到android的传参:" + arg);
			},
			goPublish(){
				console.log('阿斯顿发送到发送到发斯蒂芬斯蒂芬');
			            //将vue项目引入至安卓代码中，安卓方法暴露在window中，vue中可以直接用window去调取方法。
			            window.android.callAndroidMethod('1','2')
			        },
			showMessage(message) {
				uni.showToast({
					title: message,
					duration: 2000,
					icon: 'none'
				})
			},
			//普通纸质票
			async regular(item, qrCodeData){
				const obj = {
					aid: item.aid,
					code: '20000',
					verificationData: qrCodeData
				}
				const data = await request.post('/app-verification/v1/device/modes/allInOneMachine', obj)
				if (data.status === 200) {
					if (data.data.code === '1') {
						const objqr = {
							aid: item.aid,
							code: '20001',
							verificationData: qrCodeData,
							facePic: this.faceUrl,
							entrance: item.entroce,
							deviceSn: 'fe80::da04:fda5:cb61:'
						}
						const dataQr = await request.post('/app-verification/v1/device/modes/allInOneMachine', objqr);
						if (dataQr.status === 200) {
							this.showMessage(dataQr.message)
						}	
					} else {
						this.showMessage(data.message)
					}
				}
			},
			//登记票
			async register(item,jieStr,qrCodeData){
				const dataInfo = jieStr.split('|');
				if(dataInfo.length === 3 && dataInfo[2].length > 0){
					const cardNum = SM4Util.sm4ECBEncrypt(dataInfo[2], this.secretKey);
					const obj = {
						aid: item.aid,
						code: '10001',
						deviceSn:'fe80::da04:fda5:cb61:',
						entrance:item.entroce,
						facePic: this.faceUrl,
						allOneUserIdCard:{
							cardNum:cardNum,
							name:dataInfo[1],
							cardPic:''
						},
					}
					const data = await request.post('/app-verification/v1/device/modes/allInOneMachine', obj)
					if (data.status === 200) {
						this.showMessage(data.message)
					}
					
				}	
			},
			qrCodeApi(item, qrCodeData) {
				let miStr = qrCodeData;
				const typeData = miStr.substr(0,5);
				if(typeData === 'YfLeZ'){
					//登记模式
					const codeInfo = miStr.slice(5);
					const jieStr = SM4Util.sm4ECBDecrypt(codeInfo, this.secretKey);// 解密 SM4Util.sm4ECBDecrypt(加密后的字符串, 密钥)
					this.register(item,jieStr,qrCodeData);
				}else{
					//普通纸质票
					this.regular(item, qrCodeData)
				}
			},
			qrCodeApp(item) {
				
				
				
				let that = this;
				const infoData = getCache(yfAPIS);
				if (infoData == '') {
					uni.navigateTo({
						url: '/pages/setData/setData'
					})
				} else {
					if (infoData.hasOwnProperty('entroce') && infoData.entroce.length > 0) {
						uni.navigateTo({
							url:'/pages/activityCode/activityCode?aId='+item.aid
						})
						return;
						
						item.entroce = infoData.entroce;
						that.openQrCode(item);
					}
				}
			},
			//调用scanCode扫码
			openQrCode(item) {
				uni.scanCode({
					//是否只能从相机扫码,不能从相册扫码
					onlyFromCamera: false,
					//规定扫码类型  
					// barcode就是一维码(条形码)  qrcode就是(er)维码 
					// datamatrix就是Data Matrix码   pdf417就是PDF417条码
					scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
					// 是否启动自动识别字符编码功能
					autoDecodeCharset: false,
					// 是否开启自动放大功能 
					autoZoom: true,
					// 是否支持手动输入条形码  
					barCodeInput: false,
					success: (res) => {
						this.qrCodeApi(item, res.result);
					},
					fail: (error) => {
						//this.showMessage('唤起扫码失败！')
						console.log(error)
					}
				})
			},


			async getDataActivity() {
				console.log('data  000');

				const data = await request.get('/app-verification/v1/device/modes/getActivity', {})
				this.actList = [];
				console.log('data',data);
				if (data.status === 200) {
					let temp = this.actList;
					this.actList = temp.concat(data.data);
				}
				uni.stopPullDownRefresh();
				this.triggered = false;
			},
			scrollLower() {
				console.log('上拉刷新1');

			},
			onRefresh() {
				console.log('下拉刷新');
			},
			connect() {
				var that = this
				//h5的连接是 'ws://' + url。
				// #ifdef H5 
				that.client = this.$mqtt.connect('ws://' + MQTT_IP, MQTT_OPTIONS)
				// #endif 
				//app的连接是 'wx://' + url。
				//#ifdef MP-WEIXIN||APP-PLUS
				that.client = this.$mqtt.connect('wx://' + MQTT_IP, MQTT_OPTIONS)
				// #endif
				that.client.on('connect', function() {
					console.log('连接成功', that.client)
					//that.switches 这是订阅主题名称 和接口地址差不多，是他们定义的，直接拿过来用就行，订阅成功后，就可以接收这个的信息了
					that.client.subscribe(that.switches, function(err) {
						if (!err) {
							console.log('switches订阅成功')
						}
					});

				}).on('reconnect', function(error) {
					console.log('正在重连...', that.switches)
				}).on('error', function(error) {
					console.log('连接失败...', error)
				}).on('end', function() {
					console.log('连接断开')
				}).on('message', function(topic, message) {
					console.log('message');
					console.log(topic, message);
					// 统一接受信息。 topic 是订阅的主题名称，message是监听信息的接收和发送都能接收

				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.index {

		height: 100%;
		font-family: Source Han Sans CN-Medium, Source Han Sans CN;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #F9F8FA;
		z-index: -1;
		height: 100%;

		.main-container {
			position: relative;
			margin: 10px 40rpx;

			.noData {
				color: #353434;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
			}

			.item-box {
				display: flex;
				padding: 16rpx 0;
				align-items: center;
				position: relative;

				.headimg {
					width: 120rpx;
					height: 120rpx;
					margin: 10rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.label {
					position: absolute;
					right: 0rpx;
					top: 10rpx;
				}

				.info {
					color: #c1c0be;

					.info-name {
						color: #737272;
						margin-bottom: 12rpx;
					}
				}

			}
		}


	}

	/deep/ ::-webkit-scrollbar {
		//隐藏scroll-view里面自带的滚动条
		display: block; //必加
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: auto !important;
		background: transparent;
		overflow: auto !important;
	}
</style>